
package guoan.web.weixin;

import guoan.domain.Adv;
import guoan.domain.AdvType;
import guoan.service.AdvService;
import guoan.service.AdvTypeService;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.common.base.Preconditions;

@Controller
@RequestMapping("/weixin/adv")
public class AdvWeiXinController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvService advService;

    @Autowired
    private AdvTypeService advTypeService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(Model model){
        List<AdvType> advTypes = advTypeService
                .findByShowingOrderByOrderIdDesc(1L);
        model.addAttribute("advTypes", advTypes);
        return "/weixin/adv/index";
    }

    @RequestMapping(value = "indexGame", method = RequestMethod.GET)
    public String indexGame(@PathVariable("id") Long id,Model model){
        Preconditions.checkNotNull(id, "抱歉，参数错误！");
        Adv adv = advService.findOne(id);
        Preconditions.checkNotNull(adv, "抱歉，参数错误！");
        model.addAttribute("adv", adv);
        return "/weixin/adv/index";
    }

    @RequestMapping(value = "content/{id}", method = RequestMethod.GET)
    public String content(@PathVariable("id") Long id, Model model){

        Adv adv = advService.findOne(id);
        model.addAttribute("adv", adv);

        if (adv != null) {
            List<AdvType> advTypes = advTypeService
                    .findByShowingOrderByOrderIdDesc(1L);
            model.addAttribute("advTypes", advTypes);

            List<Adv> advForAdvTypes = advService
                    .findTop5ByAdvTypeOrderByOrderIdDesc(adv.getAdvType()
                            .getId());
            model.addAttribute("advForAdvTypes", advForAdvTypes);

        }

        return "/weixin/adv/content";
    }

    @RequestMapping(value = "list/{id}", method = RequestMethod.GET)
    public String list(@PathVariable("id") Long id, Model model){
        List<AdvType> advTypes = advTypeService
                .findByShowingOrderByOrderIdDesc(1L);
        model.addAttribute("advTypes", advTypes);

        List<Adv> advs = advService.findByAdvTypeIdOrderByOrderIdDesc(id);
        model.addAttribute("advs", advs);

        return "/weixin/adv/list";
    }

}
